***************************************************
***		DoFile for descriptive statistics		***
***		Last change: 20.12.2017 fo				***
***************************************************

set more off
capture log close
log using "$log/vert_05_descript_v13.log", replace
adopath ++ $prog


***********************************************
***		description of analysis sample 		***
***********************************************

*************************
***		1. establishments
*************************

* how many establishments?
use "$data/linked_imputed_2002-2008.dta", clear
bysort estid: keep if _n==1
tab manuf

* how many waves per establishment?
use "$data/linked_imputed_2002-2008.dta", clear
bysort estid year: keep if _n==1
bysort estid: gen wavecount = _N
mean wavecount
mean wavecount[pw=weight_nosize]

* how many persons per establishment-year?
use "$data/linked_imputed_2002-2008.dta", clear
bysort estid year: gen perscount = _N
bysort estid: keep if _n==1
mean perscount
mean perscount[pw=weight_nosize]

* descriptives

* manufacturing
matrix define estdes_manuf = J(19, 10, .)
matrix rownames estdes_manuf = leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east
matrix colnames estdes_manuf = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta" if (manuf ==1 & nocoll==1), clear
bysort estid year: keep if _n==1

qui tab gevol_cat, gen(gevol_)
	rename gevol_1 gevol_same
	rename gevol_2 gevol_more
	rename gevol_3 gevol_less
	rename gevol_4 gevol_noknow

qui tab ertragslage_py, gen(ertrag_)
	rename ertrag_1 ertrag_insu
	rename ertrag_2 ertrag_suff
	rename ertrag_3 ertrag_sati
	rename ertrag_4 ertrag_good
	rename ertrag_5 ertrag_vgoo

local count = 0
foreach var of varlist leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east {
	local ++count

	qui mean `var' [pw=weight_nosize] if year==2002
		mat estdes_manuf [`count', 1] =  _b[`var']
		mat estdes_manuf [`count', 2] = _se[`var']
		mat estdes_manuf [`count', 3] = e(N)
	qui egen min`var' = min(`var')
	qui mean min`var'
		mat estdes_manuf [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var')
	qui mean max`var'
		mat estdes_manuf [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' [pw=weight_nosize] if year==2008
		mat estdes_manuf [`count', 6] =  _b[`var']
		mat estdes_manuf [`count', 7] = _se[`var']
		mat estdes_manuf [`count', 8] = e(N)
	qui egen min`var' = min(`var')
	qui mean min`var'
		mat estdes_manuf [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var')
	qui mean max`var'
		mat estdes_manuf [`count', 10] = _b[max`var']
		drop max`var'
		}

* without weights
matrix define estdes_manuf_uw = J(19, 10, .)
matrix rownames estdes_manuf_uw = leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east
matrix colnames estdes_manuf_uw = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta"  if (manuf==1 & nocoll==1), clear
bysort estid year: keep if _n==1

qui tab gevol_cat, gen(gevol_)
	rename gevol_1 gevol_same
	rename gevol_2 gevol_more
	rename gevol_3 gevol_less
	rename gevol_4 gevol_noknow

qui tab ertragslage_py, gen(ertrag_)
	rename ertrag_1 ertrag_insu
	rename ertrag_2 ertrag_suff
	rename ertrag_3 ertrag_sati
	rename ertrag_4 ertrag_good
	rename ertrag_5 ertrag_vgoo

local count = 0
foreach var of varlist leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east {
	local ++count

	qui mean `var' if year==2002
		mat estdes_manuf_uw [`count', 1] =  _b[`var']
		mat estdes_manuf_uw [`count', 2] = _se[`var']
		mat estdes_manuf_uw [`count', 3] = e(N)
	qui egen min`var' = min(`var') if year==2002
	qui mean min`var'
		mat estdes_manuf_uw [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2002
	qui mean max`var'
		mat estdes_manuf_uw [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' if year==2008
		mat estdes_manuf_uw [`count', 6] =  _b[`var']
		mat estdes_manuf_uw [`count', 7] = _se[`var']
		mat estdes_manuf_uw [`count', 8] = e(N)
	qui egen min`var' = min(`var') if year==2008
	qui mean min`var'
		mat estdes_manuf_uw [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2008
	qui mean max`var'
		mat estdes_manuf_uw [`count', 10] = _b[max`var']
		drop max`var'
		}

* Beschreibung der Betriebsstichprobe (produz. Gewerbe, Bergbau, Energie) nach verschiedenen Merkmalen (ohne Gewichte) für die Jahre 2002 und 2008
*	Anteil Leiharbeiter, Anteil freie Mitarbeiter, Ausgliederung in diesem Jahr,
*	Exportanteil, Ertragslage, Entwicklung Geschäftsvolumen, offene Stellen, Branchentarifvertrag, Betriebsrat, verarbeitendes Gewerbe oder Bergbau, Ostdt., Zahl der soz.pflicht Beschäftigten
mat list estdes_manuf_uw

* Beschreibung der Betriebsstichprobe (produz. Gewerbe, Bergbau, Energie) nach verschiedenen Merkmalen (mit Design- und Größengewichten) für die Jahre 2002 und 2008
*	Anteil Leiharbeiter, Anteil freie Mitarbeiter, Ausgliederung in diesem Jahr,
*	Exportanteil, Ertragslage, Entwicklung Geschäftsvolumen, offene Stellen, Branchentarifvertrag, Betriebsrat, verarbeitendes Gewerbe oder Bergbau, Ostdt., Zahl der soz.pflicht Beschäftigten
mat list estdes_manuf

* other
matrix define estdes_other = J(19, 10, .)
matrix rownames estdes_other = leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east
matrix colnames estdes_other = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta" if (other==1 & nocoll==1), clear
bysort estid year: keep if _n==1

qui tab gevol_cat, gen(gevol_)
	rename gevol_1 gevol_same
	rename gevol_2 gevol_more
	rename gevol_3 gevol_less
	rename gevol_4 gevol_noknow

qui tab ertragslage_py, gen(ertrag_)
	rename ertrag_1 ertrag_insu
	rename ertrag_2 ertrag_suff
	rename ertrag_3 ertrag_sati
	rename ertrag_4 ertrag_good
	rename ertrag_5 ertrag_vgoo

local count = 0
foreach var of varlist leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east {
	local ++count

	qui mean `var' [pw=weight_nosize] if year==2002
		mat estdes_other [`count', 1] =  _b[`var']
		mat estdes_other [`count', 2] = _se[`var']
		mat estdes_other [`count', 3] = e(N)
	qui egen min`var' = min(`var')
	qui mean min`var'
		mat estdes_other [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var')
	qui mean max`var'
		mat estdes_other [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' [pw=weight_nosize] if year==2008
		mat estdes_other [`count', 6] =  _b[`var']
		mat estdes_other [`count', 7] = _se[`var']
		mat estdes_other [`count', 8] = e(N)
	qui egen min`var' = min(`var')
	qui mean min`var'
		mat estdes_other [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var')
	qui mean max`var'
		mat estdes_other [`count', 10] = _b[max`var']
		drop max`var'
		}

* without weights
matrix define estdes_other_uw = J(19, 10, .)
matrix rownames estdes_other_uw = leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east
matrix colnames estdes_other_uw = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta"  if (manuf ==1 & nocoll==1), clear
bysort estid year: keep if _n==1

qui tab gevol_cat, gen(gevol_)
	rename gevol_1 gevol_same
	rename gevol_2 gevol_more
	rename gevol_3 gevol_less
	rename gevol_4 gevol_noknow

qui tab ertragslage_py, gen(ertrag_)
	rename ertrag_1 ertrag_insu
	rename ertrag_2 ertrag_suff
	rename ertrag_3 ertrag_sati
	rename ertrag_4 ertrag_good
	rename ertrag_5 ertrag_vgoo

local count = 0
foreach var of varlist leiharb100 freie100 os_any export_proz_py ertrag_insu ertrag_suff ertrag_sati ertrag_good ertrag_vgoo gevol_same gevol_more gevol_less gevol_noknow beschgesamt persuche brantarifv betriebsrat manuf east {
	local ++count

	qui mean `var' if year==2002
		mat estdes_other_uw [`count', 1] =  _b[`var']
		mat estdes_other_uw [`count', 2] = _se[`var']
		mat estdes_other_uw [`count', 3] = e(N)
	qui egen min`var' = min(`var') if year==2002
	qui mean min`var'
		mat estdes_other_uw [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2002
	qui mean max`var'
		mat estdes_other_uw [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' if year==2008
		mat estdes_other_uw [`count', 6] =  _b[`var']
		mat estdes_other_uw [`count', 7] = _se[`var']
		mat estdes_other_uw [`count', 8] = e(N)
	qui egen min`var' = min(`var') if year==2008
	qui mean min`var'
		mat estdes_other_uw [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2008
	qui mean max`var'
		mat estdes_other_uw [`count', 10] = _b[max`var']
		drop max`var'
		}

* Beschreibung der Betriebsstichprobe (andere Branchen als produz. Gewerbe, Bergbau, Energie) nach verschiedenen Merkmalen (ohne Gewichte) für die Jahre 2002 und 2008
*	Anteil Leiharbeiter, Anteil freie Mitarbeiter, Ausgliederung in diesem Jahr,
*	Exportanteil, Ertragslage, Entwicklung Geschäftsvolumen, offene Stellen, Branchentarifvertrag, Betriebsrat, verarbeitendes Gewerbe oder Bergbau, Ostdt., Zahl der soz.pflicht Beschäftigten
mat list estdes_other_uw

* Beschreibung der Betriebsstichprobe (andere Branchen als produz. Gewerbe, Bergbau, Energie) nach verschiedenen Merkmalen (mit Design- und Größengewichten) für die Jahre 2002 und 2008
*	Anteil Leiharbeiter, Anteil freie Mitarbeiter, Ausgliederung in diesem Jahr,
*	Exportanteil, Ertragslage, Entwicklung Geschäftsvolumen, offene Stellen, Branchentarifvertrag, Betriebsrat, verarbeitendes Gewerbe oder Bergbau, Ostdt., Zahl der soz.pflicht Beschäftigten
mat list estdes_other



*********************************************************************
* Description of within-establishment variation for analysis sample *
*********************************************************************

*****************
* manufacturing *

global controls = "ib0.workertype exper_estab exper_estab2 exper_estab3 exper exper2 exper3 experxfemale exper2xfemale exper3xfemale vtxexper vtxexper2 vtxexper3"
use "$data/linked_imputed_2002-2008.dta", clear
keep if (manuf ==1 & nocoll==1)
	gen vtxexper  = vt*exper
	gen vtxexper2 = vt*exper2
	gen vtxexper3 = vt*exper3

	qui reghdfe milnwage $controls [pw=persweight], abs(i.yr_occ_edu_fe i.yr_bula_fe i.spell_fe) resid(resid) keepsing

* summarize residuals by establishment-year and reduce dataset to establishment-years
keep if e(sample) == 1
bysort estid year: egen est_resid = mean(resid)
sort estid year persnr
by estid year: keep if _n==1

* count number of establishments
by estid: gen rank = _n
egen countvar = sum(rank==1)
qui mean countvar
global Nest = _b[countvar]
drop countvar rank

* standard deviation from establishment mean
matrix define 	estdes_within_manuf = J(9 , 5, .)
matrix rownames estdes_within_manuf = est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv
matrix colnames estdes_within_manuf = 1_rownr 2_Nest 3_Nobs 4_mean 5_se

matrix define 	estdes_within_manuf_uw = J(9 , 5, .)
matrix rownames estdes_within_manuf_uw = est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv
matrix colnames estdes_within_manuf_uw = 1_rownr 2_Nest 3_Nobs 4_mean 5_se


* with weights
local rowcount = 0
foreach var in est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv {
	local ++rowcount
	by estid, sort: egen `var'_sd = sd(`var')
	qui mean `var'_sd [pw=weight_size]
		matrix estdes_within_manuf [`rowcount', 1] = `rowcount'
		matrix estdes_within_manuf [`rowcount', 2] = $Nest
		matrix estdes_within_manuf [`rowcount', 3] = e(N)
		matrix estdes_within_manuf [`rowcount', 4] = _b[`var'_sd]
		matrix estdes_within_manuf [`rowcount', 5] = _se[`var'_sd]
	}

* without weights
local rowcount = 0
foreach var in est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv {
	local ++rowcount
	qui mean `var'_sd
		matrix estdes_within_manuf_uw [`rowcount', 1] = `rowcount'
		matrix estdes_within_manuf_uw [`rowcount', 2] = $Nest
		matrix estdes_within_manuf_uw [`rowcount', 3] = e(N)
		matrix estdes_within_manuf_uw [`rowcount', 4] = _b[`var'_sd]
		matrix estdes_within_manuf_uw [`rowcount', 5] = _se[`var'_sd]
	}

* Die folgende Tabelle beschreibt die Variation verschiedener Merkmale innerhalb der Betriebe in der Analysesstichprobe (produz. Gewerbe, Bergbau, Energie)(ohne Gewichte)
* Legende:
/* Zeilen:
		est_resid: Betriebsrents
		leiharb100: Anteil Leiharbeiter
		freie100: Anteil freie Mitarbeiter
		os_panel: Ausgliederung in diesem Jahr
		export_proz_py:	Exportanteil
		ertragslage_py: Ertragslage
		lnbeschges: (log.) Zahl der soz.pflicht Beschäftigten
		persuche: offene Stellen
		brantarifv: Branchentarifvertrag

	Spalten:
		1_rownr: Zeilennummer
		2_Nest: Fallzahl Betriebe
		3_Nobs: Fallzahl Betriebs-Jahre
		4_mean: Durchschnittliche Standardabweichung (vom jeweiligen Betriebs-Durchschnitt)
		5:se: Standardfehler der durschnittlichen Standardabweichung
*/
mat list estdes_within_manuf_uw

* Die folgende Tabelle beschreibt die Variation verschiedener Merkmale innerhalb der Betriebe in der Analysesstichprobe (produz. Gewerbe, Bergbau, Energie)(mit Design- und Größengewichten)
* Legende:
/* Zeilen:
		est_resid: Betriebsrents
		leiharb100: Anteil Leiharbeiter
		freie100: Anteil freie Mitarbeiter
		os_panel: Ausgliederung in diesem Jahr
		export_proz_py:	Exportanteil
		ertragslage_py: Ertragslage
		lnbeschges: (log.) Zahl der soz.pflicht Beschäftigten
		persuche: offene Stellen
		brantarifv: Branchentarifvertrag

	Spalten:
		1_rownr: Zeilennummer
		2_Nest: Fallzahl Betriebe
		3_Nobs: Fallzahl Betriebs-Jahre
		4_mean: Durchschnittliche Standardabweichung (vom jeweiligen Betriebs-Durchschnitt)
		5:se: Standardfehler der durschnittlichen Standardabweichung
*/
mat list estdes_within_manuf


*******
* other
global controls = "ib0.workertype exper_estab exper_estab2 exper_estab3 exper exper2 exper3 experxfemale exper2xfemale exper3xfemale vtxexper vtxexper2 vtxexper3"
use "$data/linked_imputed_2002-2008.dta", clear
keep if (other==1 & nocoll==1)
	gen vtxexper  = vt*exper
	gen vtxexper2 = vt*exper2
	gen vtxexper3 = vt*exper3

	qui reghdfe milnwage $controls [pw=persweight], abs(i.yr_occ_edu_fe i.yr_bula_fe i.spell_fe) resid(resid) keepsing

* summarize residuals by establishment-year and reduce dataset to establishment-years
keep if e(sample) == 1
bysort estid year: egen est_resid = mean(resid)
sort estid year persnr
by estid year: keep if _n==1

* count number of establishments
by estid: gen rank = _n
egen countvar = sum(rank==1)
qui mean countvar
global Nest = _b[countvar]
drop countvar rank

* standard deviation from establishment mean
matrix define 	estdes_within_other = J(9 , 5, .)
matrix rownames estdes_within_other = est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv
matrix colnames estdes_within_other = 1_rownr 2_Nest 3_Nobs 4_mean 5_se

matrix define 	estdes_within_other_uw = J(9 , 5, .)
matrix rownames estdes_within_other_uw = est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv
matrix colnames estdes_within_other_uw = 1_rownr 2_Nest 3_Nobs 4_mean 5_se

local rowcount = 0
foreach var in est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv {
	local ++rowcount
	by estid, sort: egen `var'_sd = sd(`var')
	qui mean `var'_sd [pw=weight_size]
		matrix estdes_within_other [`rowcount', 1] = `rowcount'
		matrix estdes_within_other [`rowcount', 2] = $Nest
		matrix estdes_within_other [`rowcount', 3] = e(N)
		matrix estdes_within_other [`rowcount', 4] = _b[`var'_sd]
		matrix estdes_within_other [`rowcount', 5] = _se[`var'_sd]
	}

local rowcount = 0
foreach var in est_resid leiharb100 freie100 os_panel export_proz_py ertragslage_py lnbeschgesamt persuche brantarifv {
	local ++rowcount
	qui mean `var'_sd
		matrix estdes_within_other_uw [`rowcount', 1] = `rowcount'
		matrix estdes_within_other_uw [`rowcount', 2] = $Nest
		matrix estdes_within_other_uw [`rowcount', 3] = e(N)
		matrix estdes_within_other_uw [`rowcount', 4] = _b[`var'_sd]
		matrix estdes_within_other_uw [`rowcount', 5] = _se[`var'_sd]
	}

* Die folgende Tabelle beschreibt die Variation verschiedener Merkmale innerhalb der Betriebe in der Analysesstichprobe (andere Branchen als produz. Gewerbe, Bergbau, Energie)(ohne Gewichte)
* Legende:
/* Zeilen:
		est_resid: Betriebsrents
		leiharb100: Anteil Leiharbeiter
		freie100: Anteil freie Mitarbeiter
		os_panel: Ausgliederung in diesem Jahr
		export_proz_py:	Exportanteil
		ertragslage_py: Ertragslage
		lnbeschges: (log.) Zahl der soz.pflicht Beschäftigten
		persuche: offene Stellen
		brantarifv: Branchentarifvertrag

	Spalten:
		1_rownr: Zeilennummer
		2_Nest: Fallzahl Betriebe
		3_Nobs: Fallzahl Betriebs-Jahre
		4_mean: Durchschnittliche Standardabweichung (vom jeweiligen Betriebs-Durchschnitt)
		5:se: Standardfehler der durschnittlichen Standardabweichung
*/
mat list estdes_within_other_uw

* Die folgende Tabelle beschreibt die Variation verschiedener Merkmale innerhalb der Betriebe in der Analysesstichprobe (andere Branchen als produz. Gewerbe, Bergbau, Energie)(ohne Gewichte)
* Legende:
/* Zeilen:
		est_resid: Betriebsrents
		leiharb100: Anteil Leiharbeiter
		freie100: Anteil freie Mitarbeiter
		os_panel: Ausgliederung in diesem Jahr
		export_proz_py:	Exportanteil
		ertragslage_py: Ertragslage
		lnbeschges: (log.) Zahl der soz.pflicht Beschäftigten
		persuche: offene Stellen
		brantarifv: Branchentarifvertrag

	Spalten:
		1_rownr: Zeilennummer
		2_Nest: Fallzahl Betriebe
		3_Nobs: Fallzahl Betriebs-Jahre
		4_mean: Durchschnittliche Standardabweichung (vom jeweiligen Betriebs-Durchschnitt)
		5:se: Standardfehler der durschnittlichen Standardabweichung
*/
mat list estdes_within_other

******************
***		2. persons
******************

* how many persons?
use "$data/linked_imputed_2002-2008.dta", clear
bysort persnr: keep if _n==1						/* go from person-year to person level */
tab nocoll
summ persnr
summ persnr if manuf==1
summ persnr if manuf==0


* manufacturing

* descriptives, weighted
matrix define persdes_manuf = J(10, 10, .)
matrix rownames persdes_manuf = miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll
matrix colnames persdes_manuf = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta" if (manuf==1 & nocoll==1), clear
bysort persnr year: keep if _n==1
gen miwage = exp(milnwage)				/* daily wage */

qui tab workertype, gen(wtype_)
	rename wtype_1 wtype_unskill
	rename wtype_2 wtype_skbcoll
	rename wtype_3 wtype_wcoll

local count = 0
foreach var of varlist miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll {
	local ++count

	qui mean `var' [pw=persweight] if year==2002
		mat persdes_manuf [`count', 1] =  _b[`var']
		mat persdes_manuf [`count', 2] = _se[`var']
		mat persdes_manuf [`count', 3] = e(N)
	qui egen min`var' = min(`var') if year==2002
	qui mean min`var'
		mat persdes_manuf [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2002
	qui mean max`var'
		mat persdes_manuf [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' [pw=persweight] if year==2008
		mat persdes_manuf [`count', 6] =  _b[`var']
		mat persdes_manuf [`count', 7] = _se[`var']
		mat persdes_manuf [`count', 8] = e(N)
	qui egen min`var' = min(`var') if year==2008
	qui mean min`var'
		mat persdes_manuf [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2008
	qui mean max`var'
		mat persdes_manuf [`count', 10] = _b[max`var']
		drop max`var'
		}

* descriptives, unweighted
matrix define persdes_manuf_uw = J(10, 10, .)
matrix rownames persdes_manuf_uw = miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll
matrix colnames persdes_manuf_uw = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta" if (manuf==1 & nocoll==1), clear
bysort persnr year: keep if _n==1
gen miwage = exp(milnwage)				/* daily wage */

qui tab workertype, gen(wtype_)
	rename wtype_1 wtype_unskill
	rename wtype_2 wtype_skbcoll
	rename wtype_3 wtype_wcoll

local count = 0
foreach var of varlist miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll {
	local ++count

	qui mean `var' if year==2002
		mat persdes_manuf_uw [`count', 1] =  _b[`var']
		mat persdes_manuf_uw [`count', 2] = _se[`var']
		mat persdes_manuf_uw [`count', 3] = e(N)
	qui egen min`var' = min(`var') if year==2002
	qui mean min`var'
		mat persdes_manuf_uw [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2002
	qui mean max`var'
		mat persdes_manuf_uw [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' if year==2008
		mat persdes_manuf_uw [`count', 6] =  _b[`var']
		mat persdes_manuf_uw [`count', 7] = _se[`var']
		mat persdes_manuf_uw [`count', 8] = e(N)
	qui egen min`var' = min(`var') if year==2008
	qui mean min`var'
		mat persdes_manuf_uw [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2008
	qui mean max`var'
		mat persdes_manuf_uw [`count', 10] = _b[max`var']
		drop max`var'
		}

* Im Folgenden werden deskriptive Statistiken zu verschiedenen Personenmerkmalen berichtet:
* Monatslohn, Geschlecht, Ostdeutschland, 3 Bildungsgruppen, Alter

* ungewichtet
matrix list persdes_manuf_uw

* gewichtet
matrix list persdes_manuf



* other

* descriptives, weighted
matrix define persdes_other = J(10, 10, .)
matrix rownames persdes_other = miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll
matrix colnames persdes_other = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta" if (other==1 & nocoll==1), clear
bysort persnr year: keep if _n==1
gen miwage = exp(milnwage)				/* daily wage */

qui tab workertype, gen(wtype_)
	rename wtype_1 wtype_unskill
	rename wtype_2 wtype_skbcoll
	rename wtype_3 wtype_wcoll

local count = 0
foreach var of varlist miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll {
	local ++count

	qui mean `var' [pw=persweight] if year==2002
		mat persdes_other [`count', 1] =  _b[`var']
		mat persdes_other [`count', 2] = _se[`var']
		mat persdes_other [`count', 3] = e(N)
	qui egen min`var' = min(`var') if year==2002
	qui mean min`var'
		mat persdes_other [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2002
	qui mean max`var'
		mat persdes_other [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' [pw=persweight] if year==2008
		mat persdes_other [`count', 6] =  _b[`var']
		mat persdes_other [`count', 7] = _se[`var']
		mat persdes_other [`count', 8] = e(N)
	qui egen min`var' = min(`var') if year==2008
	qui mean min`var'
		mat persdes_other [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2008
	qui mean max`var'
		mat persdes_other [`count', 10] = _b[max`var']
		drop max`var'
		}

* descriptives, unweighted
matrix define persdes_other_uw = J(10, 10, .)
matrix rownames persdes_other_uw = miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll
matrix colnames persdes_other_uw = 02_mean 02_se 02_N 02_min 02_max 08_mean 08_se 08_N 08_min 08_max

use "$data/linked_imputed_2002-2008.dta" if (other==1 & nocoll==1), clear
bysort persnr year: keep if _n==1
gen miwage = exp(milnwage)				/* daily wage */

qui tab workertype, gen(wtype_)
	rename wtype_1 wtype_unskill
	rename wtype_2 wtype_skbcoll
	rename wtype_3 wtype_wcoll

local count = 0
foreach var of varlist miwage female age exper_estab exper novt vt wtype_unskill wtype_skbcoll wtype_wcoll {
	local ++count

	qui mean `var' if year==2002
		mat persdes_other_uw [`count', 1] =  _b[`var']
		mat persdes_other_uw [`count', 2] = _se[`var']
		mat persdes_other_uw [`count', 3] = e(N)
	qui egen min`var' = min(`var') if year==2002
	qui mean min`var'
		mat persdes_other_uw [`count', 4] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2002
	qui mean max`var'
		mat persdes_other_uw [`count', 5] = _b[max`var']
		drop max`var'

	qui mean `var' if year==2008
		mat persdes_other_uw [`count', 6] =  _b[`var']
		mat persdes_other_uw [`count', 7] = _se[`var']
		mat persdes_other_uw [`count', 8] = e(N)
	qui egen min`var' = min(`var') if year==2008
	qui mean min`var'
		mat persdes_other_uw [`count', 9] = _b[min`var']
		drop min`var'
	qui egen max`var' = max(`var') if year==2008
	qui mean max`var'
		mat persdes_other_uw [`count', 10] = _b[max`var']
		drop max`var'
		}

* Im Folgenden werden deskriptive Statistiken zu verschiedenen Personenmerkmalen berichtet:
* Monatslohn, Geschlecht, Ostdeutschland, 3 Bildungsgruppen, Alter

* ungewichtet
matrix list persdes_other_uw

* gewichtet
matrix list persdes_other


***************************************************************
***		Trend in two types of non-standard employment		***
***		(from all establishments, not just analysis sample)	***
***************************************************************

***********
*** All ***

forvalues year = 2002/2008 {

	use $orig/LIAB_QM2_9310_v1_bhp_basis_v1.dta if jahr == `year', clear
	keep betnr jahr w93_3_gen w73_3_gen ao_bula
	rename jahr year
	recode ao_bula (0=11)(10=7), gen(bula_basis)		/* west-berlin=berlin; saarland=rheinld.pf. for a time consistent bula variable */
	merge 1:m betnr using "$orig/LIAB_qm2_9310_v1_pers_`year'.dta", nogen
	keep if quelle==1
	bysort idnum jahr: keep if _n==1
	keep idnum year w93_3_gen w73_3_gen
	merge 1:1 idnum year using "$data/est_2002-2008.dta", nogen
	save "$data/est_2002-2008.dta", replace
	}

rename w93_3_gen w93_3_basis
recode w93_3_basis (1/20=1)(50=2)(100/145=3)(150/372=4)(400/410=5)(450/455=6) ///
	(500/527=7)(550/555=8)(600/642=9)(650/672=10)(700/748=11)(750/753=12) ///
	(800/804=13)(850/853=14)(900/930=15)(950=16)(990=17), gen(w93_1_basis)

drop if w93_3_basis==745 					/* keine überlassenden Leiharbeitsfirmen */
drop if w93_1_basis==12 | w93_1_basis==13 | w93_1_basis==16 | w93_1_basis==17 | ertragslage_py==.a	/* no public sector or education or private household or exteritorial org. or non-profit */

gen manuf = .
	replace manuf = 1 if (w93_1_basis==4 | w93_1_basis==3 | w93_1_basis==5)
	replace manuf = 0 if (w93_1_basis!=4 & w93_1_basis!=3 & w93_1_basis!=5)


*** trend

*** weighted
gen sizeweight = csweight*gesamt_sozpf
svyset estid[pw=sizeweight]

matrix define trend_all = J(7, 8, .)
matrix colnames trend_all = year N leiharb100 se freie100 se os_any se

local yrcount = 0
forvalues year = 2002/2008 {
	local yrcount = `year' - 2001
	dis " "
	dis "year:" `year'
	matrix trend_all [`yrcount', 1] = `year'
	qui mean estid if year==`year'
	matrix trend_all [`yrcount', 2] = e(N)

		local colcount = 3
		foreach var in leiharb100 freie100 os_any {
				dis "var:" "`var'" "  colcount:" "`colcount'"
				qui svy: mean `var' if year==`year'
				matrix trend_all [`yrcount', `colcount']   =  _b[`var']
				matrix trend_all [`yrcount', `colcount'+1] = _se[`var']
				local colcount = `colcount' + 2
		}
	}

* how many outsource at least once?
bysort estid: egen os_atall = max(os_panel)
bysort estid: egen os_ausgegatall = max(os_ausgeg)
	 mean os_atall os_ausgegatall
svy: mean os_atall os_ausgegatall


*** unweighted
drop sizeweight
gen sizeweight = gesamt_sozpf
svyset estid[pw=sizeweight]

matrix define trend_all_uw = J(7, 8, .)
matrix colnames trend_all_uw = year N leiharb100 se freie100 se os_any se

local yrcount = 0
forvalues year = 2002/2008 {
	local yrcount = `year' - 2001
	dis " "
	dis "year:" `year'
	matrix trend_all_uw [`yrcount', 1] = `year'
	qui mean estid if year==`year'
	matrix trend_all_uw [`yrcount', 2] = e(N)

		local colcount = 3
		foreach var in leiharb100 freie100 os_any {
				dis "var:" "`var'" "  colcount:" "`colcount'"
				qui svy: mean `var' if year==`year'
				matrix trend_all_uw [`yrcount', `colcount']   =  _b[`var']
				matrix trend_all_uw [`yrcount', `colcount'+1] = _se[`var']
				local colcount = `colcount' + 2
		}
	}

* how many outsource at least once?
bysort estid: egen os_atall_uw = max(os_panel)
bysort estid: egen os_ausgegatall_uw = max(os_ausgeg)
mean os_atall_uw os_ausgegatall_uw



*********************
*** manufacturing ***

*** Drop public sector and Leiharbeitsfirmen

use "$data/est_2002-2008.dta", clear

rename w93_3_gen w93_3_basis
recode w93_3_basis (1/20=1)(50=2)(100/145=3)(150/372=4)(400/410=5)(450/455=6) ///
	(500/527=7)(550/555=8)(600/642=9)(650/672=10)(700/748=11)(750/753=12) ///
	(800/804=13)(850/853=14)(900/930=15)(950=16)(990=17), gen(w93_1_basis)

drop if w93_3_basis==745 					/* keine überlassenden Leiharbeitsfirmen */
drop if w93_1_basis==12 | w93_1_basis==13 | w93_1_basis==16 | w93_1_basis==17 | ertragslage_py==.a	/* no public sector or education or private household or exteritorial org. or non-profit */

gen manuf = .
	replace manuf = 1 if (w93_1_basis==4 | w93_1_basis==3 | w93_1_basis==5)
	replace manuf = 0 if (w93_1_basis!=4 & w93_1_basis!=3 & w93_1_basis!=5)
keep if manuf==1

*** trend

*** weighted
gen sizeweight = csweight*gesamt_sozpf
svyset estid[pw=sizeweight]

matrix define trend_manuf = J(7, 8, .)
matrix colnames trend_manuf = year N leiharb100 se freie100 se os_any se

local yrcount = 0
forvalues year = 2002/2008 {
	local yrcount = `year' - 2001
	dis " "
	dis "year:" `year'
	matrix trend_manuf [`yrcount', 1] = `year'
	qui mean estid if year==`year'
	matrix trend_manuf [`yrcount', 2] = e(N)

		local colcount = 3
		foreach var in leiharb100 freie100 os_any {
				dis "var:" "`var'" "  colcount:" "`colcount'"
				qui svy: mean `var' if year==`year'
				matrix trend_manuf [`yrcount', `colcount']   =  _b[`var']
				matrix trend_manuf [`yrcount', `colcount'+1] = _se[`var']
				local colcount = `colcount' + 2
		}
	}

* how many outsource at least once?
bysort estid: egen os_atall_manuf = max(os_panel)
bysort estid: egen os_ausgegatall_manuf = max(os_ausgeg)
	 mean os_atall_manuf os_ausgegatall_manuf
svy: mean os_atall_manuf os_ausgegatall_manuf


*** unweighted
drop sizeweight
gen sizeweight = gesamt_sozpf
svyset estid[pw=sizeweight]

matrix define trend_manuf_uw = J(7, 8, .)
matrix colnames trend_manuf_uw = year N leiharb100 se freie100 se os_any se

local yrcount = 0
forvalues year = 2002/2008 {
	local yrcount = `year' - 2001
	dis " "
	dis "year:" `year'
	matrix trend_manuf_uw [`yrcount', 1] = `year'
	qui mean estid if year==`year'
	matrix trend_manuf_uw [`yrcount', 2] = e(N)

		local colcount = 3
		foreach var in leiharb100 freie100 os_any {
				dis "var:" "`var'" "  colcount:" "`colcount'"
				qui svy: mean `var' if year==`year'
				matrix trend_manuf_uw [`yrcount', `colcount']   =  _b[`var']
				matrix trend_manuf_uw [`yrcount', `colcount'+1] = _se[`var']
				local colcount = `colcount' + 2
		}
	}

* how many outsource at least once?
bysort estid: egen os_atall_manuf_uw = max(os_panel)
bysort estid: egen os_ausgegatall_manuf_uw = max(os_ausgeg)
	 mean os_atall_manuf_uw os_ausgegatall_manuf_uw
svy: mean os_atall_manuf_uw os_ausgegatall_manuf_uw


*************************
*** non-manufacturing ***

*** Drop public sector and Leiharbeitsfirmen

use "$data/est_2002-2008.dta", clear

rename w93_3_gen w93_3_basis
recode w93_3_basis (1/20=1)(50=2)(100/145=3)(150/372=4)(400/410=5)(450/455=6) ///
	(500/527=7)(550/555=8)(600/642=9)(650/672=10)(700/748=11)(750/753=12) ///
	(800/804=13)(850/853=14)(900/930=15)(950=16)(990=17), gen(w93_1_basis)

drop if w93_3_basis==745 					/* keine überlassenden Leiharbeitsfirmen */
drop if w93_1_basis==12 | w93_1_basis==13 | w93_1_basis==16 | w93_1_basis==17 | ertragslage_py==.a	/* no public sector or education or private household or exteritorial org. or non-profit */

gen manuf = .
	replace manuf = 1 if (w93_1_basis==4 | w93_1_basis==3 | w93_1_basis==5)
	replace manuf = 0 if (w93_1_basis!=4 & w93_1_basis!=3 & w93_1_basis!=5)
keep if manuf==0

*** trend

*** weighted
gen sizeweight = csweight*gesamt_sozpf
svyset estid[pw=sizeweight]

matrix define trend_other = J(7, 8, .)
matrix colnames trend_other = year N leiharb100 se freie100 se os_any se

local yrcount = 0
forvalues year = 2002/2008 {
	local yrcount = `year' - 2001
	dis " "
	dis "year:" `year'
	matrix trend_other [`yrcount', 1] = `year'
	qui mean estid if year==`year'
	matrix trend_other [`yrcount', 2] = e(N)

		local colcount = 3
		foreach var in leiharb100 freie100 os_any {
				dis "var:" "`var'" "  colcount:" "`colcount'"
				qui svy: mean `var' if year==`year'
				matrix trend_other [`yrcount', `colcount']   =  _b[`var']
				matrix trend_other [`yrcount', `colcount'+1] = _se[`var']
				local colcount = `colcount' + 2
		}
	}

* how many outsource at least once?
bysort estid: egen os_atall_other = max(os_panel)
bysort estid: egen os_ausgegatall_other = max(os_ausgeg)
	 mean os_atall_other os_ausgegatall_other
svy: mean os_atall_other os_ausgegatall_other

*** unweighted
drop sizeweight
gen sizeweight = gesamt_sozpf
svyset estid[pw=sizeweight]

matrix define trend_other_uw = J(7, 8, .)
matrix colnames trend_other_uw = year N leiharb100 se freie100 se os_any se

local yrcount = 0
forvalues year = 2002/2008 {
	local yrcount = `year' - 2001
	dis " "
	dis "year:" `year'
	matrix trend_other_uw [`yrcount', 1] = `year'
	qui mean estid if year==`year'
	matrix trend_other_uw [`yrcount', 2] = e(N)

		local colcount = 3
		foreach var in leiharb100 freie100 os_any {
				dis "var:" "`var'" "  colcount:" "`colcount'"
				qui svy: mean `var' if year==`year'
				matrix trend_other_uw [`yrcount', `colcount']   =  _b[`var']
				matrix trend_other_uw [`yrcount', `colcount'+1] = _se[`var']
				local colcount = `colcount' + 2
		}
	}

* how many outsource at least once?
bysort estid: egen os_atall_other_uw = max(os_panel)
bysort estid: egen os_ausgegatall_other_uw = max(os_ausgeg)
	 mean os_atall_other_uw os_ausgegatall_other_uw
svy: mean os_atall_other_uw os_ausgegatall_other_uw


*** Im Folgenden wird der Anteil verschiedener Beschaeftigungsformen für die Jahre 2002 bis 2008 berichtet
*** Leiharbeiter, freie Mitarbeiter)
* 	gewichtet

* Alle Branchen
mat list trend_all

* Nur verarb. Gewerbe, Bergbau, Energie
mat list trend_manuf

* Alle Branchen außer verarb. Gewerbe, Bergbau, Energie
mat list trend_other

*** Im Folgenden wird der Anteil verschiedener Beschaeftigungsformen für die Jahre 2002 bis 2008 berichtet
*** (Leiharbeiter, freie Mitarbeiter, Outsourcing)
* 	ungewichtet

* Alle Branchen
mat list trend_all_uw

* Nur verarb. Gewerbe, Bergbau, Energie
mat list trend_manuf_uw

* Alle Branchen außer verarb. Gewerbe, Bergbau, Energie
mat list trend_other_uw


*** END ***
log close




























